package net.sf.odinms.server;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.odinms.database.DatabaseConnection;

import net.sf.odinms.provider.MapleData;
import net.sf.odinms.provider.MapleDataProvider;
import net.sf.odinms.provider.MapleDataProviderFactory;
import net.sf.odinms.provider.MapleDataTool;
import net.sf.odinms.tools.StringUtil;

/**
 *
 * @author Lerk
 */
public class CashItemFactory {

    private static Map<Integer, Integer> snLookup = new HashMap<Integer, Integer>();
    private static Map<Integer, Integer> idLookup = new HashMap<Integer, Integer>();
    private static Map<Integer, CashItemInfo> itemStats = new HashMap<Integer, CashItemInfo>();
    private static MapleDataProvider data = MapleDataProviderFactory.getDataProvider(new File(System.getProperty("net.sf.odinms.wzpath") + "/Etc.wz"));
    private static MapleData commodities = data.getData(StringUtil.getLeftPaddedStr("Commodity.img", '0', 11));
    private static Map<Integer, List<CashItemInfo>> cashPackages = new HashMap<Integer, List<CashItemInfo>>();
    
    private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CashItemFactory.class);
    private static Map<Integer, CashItemInfo> getItemInSql = new HashMap<Integer, CashItemInfo>();

    public static CashItemInfo getItemInSql(int sn) {
        CashItemInfo stats = getItemInSql.get(sn);
        if (stats == null) {
            try {
                //先获取制造该物品需要的 人物等级 锻造技能等级 金钱 锻造出来的物品的数量
                Connection con = DatabaseConnection.getConnection();
                PreparedStatement ps = con.prepareStatement("SELECT * FROM cashshopcheck WHERE SN = ?");
                ps.setInt(1, sn);
                ResultSet rs = ps.executeQuery();
                int itemId = 0;
                int count = 0;
                int price = 0;
                int period = 0; //获得的item数量
                int gender = 0;//促进剂
                int onSale1 = 0;
                if (rs.next()) {
                    itemId = rs.getInt("itemId");
                    count = rs.getInt("count");
                    price = rs.getInt("price");
                    period = rs.getInt("period");
                    gender = rs.getInt("gender");
                    onSale1 = rs.getInt("onSale");
                } else {
                    System.out.println("cashshopcheck表中没有此物品 请添加："+sn);
                }
                ps.close();
                rs.close();
                //如果onSale 值为1  就是不可销售
                //如果onSale 值为0  就是可销售
                boolean onSale = onSale1 == 0;
                stats = new CashItemInfo(sn, itemId, count, price, period, gender, onSale);
                getItemInSql.put(sn, stats);
            } catch (SQLException sqle) {
                log.error("从Sql中获取商城数据出错", sqle);
            }
        }
        return stats;
    }

    public static CashItemInfo getItem(int sn) {
        CashItemInfo stats = itemStats.get(sn);
        if (stats == null) {
            int cid = getCommodityFromSN(sn);
            int itemId = MapleDataTool.getIntConvert(cid + "/ItemId", commodities);
            int count = MapleDataTool.getIntConvert(cid + "/Count", commodities, 1);
            int price = MapleDataTool.getIntConvert(cid + "/Price", commodities, 0);
            int period = MapleDataTool.getIntConvert(cid + "/Period", commodities, 0);
            int gender = MapleDataTool.getIntConvert(cid + "/Gender", commodities, 2);
            boolean onSale = MapleDataTool.getIntConvert(cid + "/OnSale", commodities, 0) == 1;
            stats = new CashItemInfo(sn, itemId, count, price, period, gender, onSale);
            itemStats.put(sn, stats);
        }
        return stats;
    }
    
    private static int getCommodityFromSN(int sn) {
        int cid;
        if (snLookup.get(sn) == null) {
            int curr = snLookup.size() - 1;
            int currSN = 0;
            if (curr == -1) {
                curr = 0;
                currSN = MapleDataTool.getIntConvert("0/SN", commodities);
                snLookup.put(currSN, curr);

            }
            for (int i = snLookup.size() - 1; currSN != sn; i++) {
                curr = i;
                currSN = MapleDataTool.getIntConvert(curr + "/SN", commodities);
                snLookup.put(currSN, curr);
            }
            cid = curr;
        } else {
            cid = snLookup.get(sn);
        }
        return cid;
    }

    public static List<CashItemInfo> getPackageItems(int itemId) {
        if (cashPackages.containsKey(itemId)) {
            return cashPackages.get(itemId);
        }
        List<CashItemInfo> packageItems = new ArrayList<CashItemInfo>();
        MapleDataProvider dataProvider = MapleDataProviderFactory.getDataProvider(new File(System.getProperty("net.sf.odinms.wzpath") + "/" + "Etc.wz"));
        MapleData a = dataProvider.getData("CashPackage.img");
        for (MapleData b : a.getChildren()) { //getChildren 子项名
            if (itemId == Integer.parseInt(b.getName())) { //9100000
                for (MapleData c : b.getChildren()) { //SN
                    for (MapleData d : c.getChildren()) { // 0 1 2 3
                        //很麻烦的取值
                        int SN = MapleDataTool.getIntConvert("" + Integer.parseInt(d.getName()), c);
                        packageItems.add(getItem(SN));
                    }
                }
                break;
            }
        }
        cashPackages.put(itemId, packageItems);
        return packageItems;
    }

    public static int getSnFromId(int id) {
        int cid;
        if (idLookup.get(id) == null) {
            int curr = idLookup.size() - 1;
            int currSN = 0;
            if (curr == -1) {
                curr = 0;
                currSN = MapleDataTool.getIntConvert("0/ItemId", commodities);
                idLookup.put(currSN, curr);
            }
            for (int i = idLookup.size() - 1; currSN != id; i++) {
                curr = i;
                currSN = MapleDataTool.getIntConvert(curr + "/ItemId", commodities);
                idLookup.put(currSN, curr);
            }
            cid = curr;
        } else {
            cid = idLookup.get(id);
        }
        return MapleDataTool.getIntConvert(cid + "/SN", commodities);
    }
}